HTMLify
script.js
Views: 10 | Author: cody
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | function pop (e) { let amount = 30; switch (e.target.dataset.type) { case 'shadow': case 'line': amount = 60; break; } // Quick check if user clicked the button using a keyboard if (e.clientX === 0 && e.clientY === 0) { const bbox = e.target.getBoundingClientRect(); const x = bbox.left + bbox.width / 2; const y = bbox.top + bbox.height / 2; for (let i = 0; i < 30; i++) { // We call the function createParticle 30 times // We pass the coordinates of the button for x & y values createParticle(x, y, e.target.dataset.type); } } else { for (let i = 0; i < amount; i++) { createParticle(e.clientX, e.clientY + window.scrollY, e.target.dataset.type); } } } function createParticle (x, y, type) { const particle = document.createElement('particle'); document.body.appendChild(particle); let width = Math.floor(Math.random() * 30 + 8); let height = width; let destinationX = (Math.random() - 0.5) * 300; let destinationY = (Math.random() - 0.5) * 300; let rotation = Math.random() * 520; let delay = Math.random() * 200; switch (type) { case 'square': particle.style.background = `hsl(${Math.random() * 90 + 270}, 70%, 60%)`; particle.style.border = '1px solid white'; break; case 'emoji': particle.innerHTML = ['โค','๐งก','๐','๐','๐','๐','๐ค'][Math.floor(Math.random() * 7)]; particle.style.fontSize = `${Math.random() * 24 + 10}px`; width = height = 'auto'; break; case 'mario': particle.style.backgroundImage = 'url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/127738/mario-face.png)'; break; case 'shadow': var color = `hsl(${Math.random() * 90 + 90}, 70%, 50%)`; particle.style.boxShadow = `0 0 ${Math.floor(Math.random() * 10 + 10)}px ${color}`; particle.style.background = color; particle.style.borderRadius = '50%'; width = height = Math.random() * 5 + 4; break; case 'line': var color = `hsl(${Math.random() * 90 + 90}, 70%, 50%)`; particle.style.background = 'black'; height = 1; rotation += 1000; delay = Math.random() * 1000; break; } particle.style.width = `${width}px`; particle.style.height = `${height}px`; const animation = particle.animate([ { transform: `translate(-50%, -50%) translate(${x}px, ${y}px) rotate(0deg)`, opacity: 1 }, { transform: `translate(-50%, -50%) translate(${x + destinationX}px, ${y + destinationY}px) rotate(${rotation}deg)`, opacity: 0 } ], { duration: Math.random() * 1000 + 5000, easing: 'cubic-bezier(0, .9, .57, 1)', delay: delay }); animation.onfinish = removeParticle; } function removeParticle (e) { e.srcElement.effect.target.remove(); } if (document.body.animate) { document.querySelectorAll('button').forEach(button => button.addEventListener('click', pop)); } |